namespace Creek.UI.Tabstrip.Control
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    using Creek.UI.Tabstrip.Design;

    [Designer(typeof(FATabStripItemDesigner))]
    [ToolboxItem(false)]
    [DefaultProperty("Title")]
    [DefaultEvent("Changed")]
    public class FATabStripItem : Panel
    {
        #region Events

        public event EventHandler Changed;

        #endregion

        #region Fields

        //private DrawItemState drawState = DrawItemState.None;
        private bool canClose = true;

        private Image image;

        private bool isDrawn;

        private bool selected;

        private RectangleF stripRect = Rectangle.Empty;

        private string title = string.Empty;

        private bool visible = true;

        #endregion

        #region Props

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new Size Size
        {
            get
            {
                return base.Size;
            }
            set
            {
                base.Size = value;
            }
        }

        [DefaultValue(true)]
        public new bool Visible
        {
            get
            {
                return this.visible;
            }
            set
            {
                if (this.visible == value)
                {
                    return;
                }

                this.visible = value;
                this.OnChanged();
            }
        }

        internal RectangleF StripRect
        {
            get
            {
                return this.stripRect;
            }
            set
            {
                this.stripRect = value;
            }
        }

        [Browsable(false)]
        [DefaultValue(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool IsDrawn
        {
            get
            {
                return this.isDrawn;
            }
            set
            {
                if (this.isDrawn == value)
                {
                    return;
                }

                this.isDrawn = value;
            }
        }

        /// <summary>
        /// 	Image of <see cref="FATabStripItem" /> which will be displayed
        /// 	on menu items.
        /// </summary>
        [DefaultValue(null)]
        public Image Image
        {
            get
            {
                return this.image;
            }
            set
            {
                this.image = value;
            }
        }

        [DefaultValue(true)]
        public bool CanClose
        {
            get
            {
                return this.canClose;
            }
            set
            {
                this.canClose = value;
            }
        }

        [DefaultValue("Name")]
        public string Title
        {
            get
            {
                return this.title;
            }
            set
            {
                if (this.title == value)
                {
                    return;
                }

                this.title = value;
                this.OnChanged();
            }
        }

        /// <summary>
        /// 	Gets and sets a value indicating if the page is selected.
        /// </summary>
        [DefaultValue(false)]
        [Browsable(false)]
        public bool Selected
        {
            get
            {
                return this.selected;
            }
            set
            {
                if (this.selected == value)
                {
                    return;
                }

                this.selected = value;
            }
        }

        [Browsable(false)]
        public string Caption
        {
            get
            {
                return this.Title;
            }
        }

        #endregion

        #region Ctor

        public FATabStripItem()
            : this(string.Empty, null)
        {
        }

        public FATabStripItem(System.Windows.Forms.Control displayControl)
            : this(string.Empty, displayControl)
        {
        }

        public FATabStripItem(string caption, System.Windows.Forms.Control displayControl)
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.ContainerControl, true);

            this.selected = false;
            this.Visible = true;

            this.UpdateText(caption, displayControl);

            //Add to controls
            if (displayControl != null)
            {
                this.Controls.Add(displayControl);
            }
        }

        #endregion

        #region IDisposable

        /// <summary>
        /// 	Handles proper disposition of the tab page control.
        /// </summary>
        /// <param name="disposing"> </param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (disposing)
            {
                if (this.image != null)
                {
                    this.image.Dispose();
                }
            }
        }

        #endregion

        #region ShouldSerialize

        public bool ShouldSerializeIsDrawn()
        {
            return false;
        }

        public bool ShouldSerializeDock()
        {
            return false;
        }

        public bool ShouldSerializeControls()
        {
            return this.Controls != null && this.Controls.Count > 0;
        }

        public bool ShouldSerializeVisible()
        {
            return true;
        }

        #endregion

        #region Methods

        private void UpdateText(string caption, System.Windows.Forms.Control displayControl)
        {
            if (displayControl != null && displayControl is ICaptionSupport)
            {
                var capControl = displayControl as ICaptionSupport;
                this.Title = capControl.Caption;
            }
            else if (caption.Length <= 0 && displayControl != null)
            {
                this.Title = displayControl.Text;
            }
            else if (caption != null)
            {
                this.Title = caption;
            }
            else
            {
                this.Title = string.Empty;
            }
        }

        public void Assign(FATabStripItem item)
        {
            this.Visible = item.Visible;
            this.Text = item.Text;
            this.CanClose = item.CanClose;
            this.Tag = item.Tag;
        }

        protected internal virtual void OnChanged()
        {
            if (this.Changed != null)
            {
                this.Changed(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// 	Return a string representation of page.
        /// </summary>
        /// <returns> </returns>
        public override string ToString()
        {
            return this.Caption;
        }

        #endregion
    }
}